package com.hui.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;


@Configuration
public class CorsConfig {

    public CorsConfig(){

    }

    @Bean
    public CorsFilter corsFilter(){
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("http://localhost:8080");

        config.setAllowCredentials(true);   //是否发送Cookie信息
        config.addAllowedMethod("*");   //放行的方法
        config.addAllowedHeader("*");   //设置允许的header
        //url添加映射的路径
        UrlBasedCorsConfigurationSource corsSource = new UrlBasedCorsConfigurationSource();
        corsSource.registerCorsConfiguration("/**",config);     //路由网关

        //返回路由设置
        return new CorsFilter(corsSource);
    }
}
